<?php
class Zuoye extends MY_Controller
{	
	function __construct() {

        parent::__construct();
    }


//     function get_list(){
//         $data = $this->user_post_data;
//         if(!empty($data)){
//             $sql = "SELECT zy.*,sub.name as subject_name,sub.img FROM tiku_zuoye zy left join tiku_b_subject sub on sub.id = zy.subject_id where zy.user_id={$data['user_token']['user_id']}";

//             $result = $this->db->query($sql)->result_array();
            
//             $data_obj = array(
//                 'count' => count($result),
//                 'list' => $result,
//              );
//             echo json_encode(array("code" => 1, "data" => $data_obj));   // 
//              // echo $userid;
//         }else{
//             echo json_encode(array("code" => 404, "data" => 'not data'));
//         }
//     }

//     //根据孩子id取作业list
//     function get_list_by_child(){
//         $data = $this->user_post_data;
//         $this->load->model("Zuoyes");
//         $conditions['child_id'] = $data['child_id'];
//         $conditions['time_type'] = 1;
//         $today_zuoye_list = $this->Zuoyes->get_child_zuoyes(0, 0, $conditions); //今日作业

//         $conditions['time_type'] = 2;
//         $limit = !empty($data['limit']) ? $data['limit'] : 0;
//         $past_zuoye_list = $this->Zuoyes->get_child_zuoyes($limit * $this->offset, $this->offset, $conditions);//往期作业
//         $past_zuoye_list['total_page'] = ceil($past_zuoye_list['count'] / $this->offset);

//         $reward_list = $this->Zuoyes->get_child_zuoye_rewards(0, 3, $conditions);

//         echo json_encode(array("code" => 1, "data" => array("today_zuoye_list" => $today_zuoye_list, "past_zuoye_list" => $past_zuoye_list, "reward_list" => $reward_list)));
//     }

//     function add(){
//         $data = $this->user_post_data;
//         if(!empty($data)){
//             $zuoye = array(
//                 'childer_id' => $data['childer_id'], 
//                 'user_id' => $data['user_token']['user_id'], 
//                 'subject_id' => $data['subject_id'], 
//                 'title' => $data['title'], 
//                 'zuoye_type' => $data['zuoye_type'],
//                 'zuoye_week' => date('N', strtotime($data['zuoye_time'])), 
//                 'zuoye_time' => $data['zuoye_time'], 
//                 'status' => 0, 
//                 'reward_stars' => 0, 
//                 'submit_time' => 0, 
//                 'checked_time' => 0, 
//                 'add_time' => time(), 
//             );
//             $this->db->insert('tiku_zuoye',$zuoye);
            
//             echo json_encode(array("code" => 1, "data" => null));   // 
//              // echo $userid;
//         }else{
//             echo json_encode(array("code" => 404, "data" => 'not data'));
//         }
//     }

//     function submit(){
//         $data = $this->user_post_data;
//         if(!empty($data)){
//             $today = date('Y-m-d',time());
//             $zuoye = array(
//                 'status' => 1, 
//                 'submit_time' => time()
//             );
//             $this->db->update('tiku_zuoye',$zuoye, array('id' => $data['zuoye_id']));
            
//             echo json_encode(array("code" => 1, "data" => null));   // 
//              // echo $userid;
//         }else{
//             echo json_encode(array("code" => 404, "data" => 'not data'));
//         }
//     }

//     // function check_stars(){
//     //     $data = file_get_contents("php://input");
//     //     if(!empty($data)){
//     //         $data = json_decode($data,true);
//     //         $time = time();
//     //         $stars = $data['stars'];
//     //         $id = $data['zuoye_id'];

//     //         $sql = "UPDATE tiku_zuoye SET reward_stars = (reward_stars + {$stars}),checked_time='{$time}',status=2 WHERE id = {$id}";
//     //         $this->db->query($sql);

            
            
//     //         echo json_encode(array("code" => 1, "data" => null));   // 
//     //          // echo $userid;
//     //     }else{
//     //         echo json_encode(array("code" => 404, "data" => 'not data'));
//     //     }
//     // }

//     function get_zuoye_detail(){
//         $data = $this->user_post_data;
//         if(!empty($data)){           
//             $sql = "SELECT zy.*,sub.name as subject_name,sub.img FROM tiku_zuoye zy left join tiku_b_subject sub on sub.id = zy.subject_id where zy.id={$data['zuoye_id']}";

//             $result = $this->db->query($sql)->row_array();
//             $result['submit_time'] = $today = date('Y-m-d H:s',$result['submit_time']);
            
//             echo json_encode(array("code" => 1, "data" => $result));   // 
//              // echo $userid;
//         }else{
//             echo json_encode(array("code" => 404, "data" => 'not data'));
//         }
//     }

//     function check(){
//         $data = $this->user_post_data;
//         if(!empty($data)){
//             $time = time();
//             $id = $data['zuoye_id'];
//             $zuoye_info = get_field_by_where("zuoye", "id,user_id,childer_id,status,subject_id", "id='{$id}'");
//             if(!$zuoye_info){
//                 echo json_encode(array("code" => 0, "data" => '数据不存在'));
//                 exit;
//             }
//             if($zuoye_info['user_id'] != $data['user_token']['user_id']){
//                 echo json_encode(array("code" => 0, "data" => '用户不匹配'));
//                 exit;
//             }
//             /*if($zuoye_info['status'] == 2){
//                 echo json_encode(array("code" => 0, "data" => '已经检查过了，不能重复检查'));
//                 exit;
//             }*/
//             if($zuoye_info['user_id'] != $data['user_token']['user_id']){
//                 echo json_encode(array("code" => 0, "data" => 'user is not matching'));
//                 exit;
//             }
//             $image = !empty($data['image']) ? get_qiniu_url($data['image']) : '';
//             $zy = array(
//                 'reward_stars' => $data['stars'],
//                 'comment' => $data['comment'],
//                 'image' => $image,
//                 'checked_time' => $time,
//                 'status'  => 2
//             );
//             $this->db->update('zuoye', $zy, array('id' => $id));
//             if($this->db->affected_rows() > 0){
//                 //用户动态数据
//                 //查询是否存在对应的动态数据
//                 $user_dynamic_info = get_field_by_where("user_dynamics", "id", "child_id='{$zuoye_info['childer_id']}' AND type = 2 AND data_id='{$id}'");
//                 $user_dynamic_data['user_id']       = $data['user_token']['user_id'];
//                 $user_dynamic_data['child_id']      = $zuoye_info['childer_id'];
//                 $user_dynamic_data['type']          = 2;
//                 $user_dynamic_data['data_id']       = $id;
//                 $user_dynamic_data['category_id']   = $zuoye_info['subject_id'];
//                 $user_dynamic_data['stars']         = $data['stars'];
//                 $user_dynamic_data['text']          = $data['comment'];
//                 $user_dynamic_data['image']         = $image;
//                 $user_dynamic_data['add_time']      = $time;
//                 if(!empty($user_dynamic_info['id'])){
//                     //更新动态数据
//                     $this->db->update("user_dynamics", $user_dynamic_data, array("id" => $user_dynamic_info['id']));
//                 }else{
//                     //写入动态数据
//                     $this->db->insert("user_dynamics", $user_dynamic_data);
//                 }

//                 $this->db->set("stars", "stars + " . $data['stars'], false);
//                 $this->db->where("id", $zuoye_info['childer_id']);
//                 $this->db->update("user_childrens");
                
//                 $today_stars_info = get_field_by_where("user_children_today_stars", "*", "child_id='{$zuoye_info['childer_id']}'");
//                 if(!$today_stars_info){
//                     $today_stars['user_id'] = $data['user_token']['user_id'];
//                     $today_stars['child_id'] = $zuoye_info['childer_id'];
//                     $today_stars['stars'] = $data['stars'];
//                     $today_stars['last_time'] = $time;
//                     $this->db->insert("user_children_today_stars", $today_stars);
//                 }else{
//                     $today_time = strtotime(date('Y-m-d', $time));
//                     if($today_stars_info['last_time'] >= $today_time){
//                         $this->db->set("stars", "stars + " . $data['stars'], false);
//                         $this->db->set("last_time", $time);
//                         $this->db->where("id", $today_stars_info['id']);
//                         $this->db->update("user_children_today_stars");
//                     }else{
//                         $this->db->update("user_children_today_stars", array("stars" => $data['stars'], "last_time" => $time), array("id" => $today_stars_info['id']));
//                     }
//                 }
//             }
//             echo json_encode(array("code" => 1, "data" => null));   // 
//              // echo $userid;
//         }else{
//             echo json_encode(array("code" => 404, "data" => 'not data'));
//         }
//     }

//     function del(){
//         $data = $this->user_post_data;
//         if(!empty($data)){          
//             $id = $data['zuoye_id'];
//             $sql = "DELETE FROM tiku_zuoye where id = $id";
//             $this->db->query($sql);     
//             echo json_encode(array("code" => 1, "data" => null));   // 
//              // echo $userid;
//         }else{
//             echo json_encode(array("code" => 404, "data" => 'not data'));
//         }
//     }



//     function get_curr_week(){
//         $week_start_time = strtotime(date('Y-m-d', time())) - date('w',strtotime(date('Y-m-d', time())))*86400 + 3600 * 24;
//         $weeks = array("周一", "周二", "周三", "周四", "周五", "周六", "周日");

//         $today_week = date('N', time());

//         $week_data = array();
//         for($i = 0; $i < 7; $i++){
//             $week_data[$i]['id'] = $i + 1;
//             $today_start_time = $week_start_time + 3600 * 24 * $i;
//             $week_data[$i]['week'] = $weeks[$i];
//             $week_data[$i]['date'] = date("Y-m-d", $today_start_time);
//             if($today_week == ($i+1)){
//                 $week_data[$i]['is_today'] = 1;
//             }else{
//                 $week_data[$i]['is_today'] = 0;
//             }
//         }
//         echo json_encode(array("code" => 1, "data" => $week_data));
        
//     }


//     /**
//      * 获取一周的作业
//      * @return [type] [description]
//      */
//     function get_week_zuoye_list_by_child(){
//         $data = $this->user_post_data;
//         if(!empty($data)){
//             //本周星期一
//             $week_start_time = strtotime(date('Y-m-d', time())) - date('w',strtotime(date('Y-m-d', time())))*86400 + 3600 * 24;
//             $week_start_date = date("Y-m-d", $week_start_time);

//             //本周星期天
//             $week_end_time = $week_start_time + 3600 * 24 * 7 - 1;
//             $week_end_date = date("Y-m-d", $week_end_time);
           
            
//             //查询数据

//             $sql = "SELECT zy.*,sub.name as subject_name,sub.img FROM tiku_zuoye zy left join tiku_b_subject sub on sub.id = zy.subject_id where zy.childer_id={$data['childer_id']} and zuoye_time >= '{$week_start_date}' and zuoye_time <= '{$week_end_date}' order by zuoye_week asc,id desc";
//             $result = $this->db->query($sql)->result_array();

            
//             //处理数据
//             $monday = array('week'=>1,'date'=>$week_start_date,'count'=>0,'list'=>array());
           

//             $tuesday = array('week'=>2,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 1),'count'=>0,'list'=>array());


//             $wendnesday = array('week'=>3,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 2),'count'=>0,'list'=>array());
//             $thursday = array('week'=>4,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 3),'count'=>0,'list'=>array());
//             $firday = array('week'=>5,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 4),'count'=>0,'list'=>array());
//             $saturday = array('week'=>6,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 5),'count'=>0,'list'=>array());
//             $sunday = array('week'=>7,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 6),'count'=>0,'list'=>array());

//             foreach ($result as $key => $value) {

//                 if($value['zuoye_week'] == 1){
                    
//                     $monday['list'][] = $value;
//                 }else if($value['zuoye_week'] == 2){
                   
//                     $tuesday['list'][] = $value;
//                 }else if($value['zuoye_week'] == 3){
                    
//                     $wendnesday['list'][] = $value;
//                 }else if($value['zuoye_week'] == 4){
                    
//                     $thursday['list'][] = $value;
//                 }else if($value['zuoye_week'] == 5){
                    
//                     $firday['list'][] = $value;
//                 }else if($value['zuoye_week'] == 6){
                    
//                     $saturday['list'][] = $value;
//                 }else if($value['zuoye_week'] == 7){
                   
//                     $sunday['list'][] = $value;
//                 }
//             }

//             $monday['count'] = !empty($monday['list']) ? count($monday['list']):0;
//             $tuesday['count'] = !empty($tuesday['list']) ? count($tuesday['list']):0;
//             $wendnesday['count'] = !empty($wendnesday['list']) ? count($wendnesday['list']):0;
//             $thursday['count'] = !empty($thursday['list']) ? count($thursday['list']):0;
//             $firday['count'] = !empty($firday['list']) ? count($firday['list']):0;
//             $saturday['count'] = !empty($saturday['list']) ? count($saturday['list']):0;
//             $sunday['count'] = !empty($sunday['list']) ? count($sunday['list']):0;

            
//             $data_obj = array(
//                 'count' => count($result),
//                 'list' => array(
//                     $monday,
//                     $tuesday,
//                     $wendnesday,
//                     $thursday,
//                     $firday,
//                     $saturday,
//                     $sunday),
//              );

//             echo json_encode(array("code" => 1, "data" => $data_obj));
//         }else{
//             echo json_encode(array("code" => 404, "data" => "null"));
//         }
//     }
// =======
// 		parent::__construct();
// 	}


	function get_list(){
		$data = $this->user_post_data;
		if(!empty($data)){
			$sql = "SELECT zy.*,sub.name as subject_name,sub.img FROM tiku_zuoye zy left join tiku_b_subject sub on sub.id = zy.subject_id where zy.user_id={$data['user_token']['user_id']}";

			$result = $this->db->query($sql)->result_array();
			
			$data_obj = array(
				'count' => count($result),
				'list' => $result,
			 );
			echo json_encode(array("code" => 1, "data" => $data_obj));   // 
			 // echo $userid;
		}else{
			echo json_encode(array("code" => 404, "data" => 'not data'));
		}
	}

	//根据孩子id取作业list
	function get_list_by_child(){
		$data = $this->user_post_data;
		$this->load->model("Zuoyes");
		$conditions['child_id'] = $data['child_id'];
		$conditions['time_type'] = 1;
		$today_zuoye_list = $this->Zuoyes->get_child_zuoyes(0, 0, $conditions); //今日作业

		$conditions['time_type'] = 2;
		$limit = !empty($data['limit']) ? $data['limit'] : 0;
		$this->offset = 1;
		$past_zuoye_list = $this->Zuoyes->get_child_zuoyes($limit * $this->offset, $this->offset, $conditions);//往期作业
		$past_zuoye_list['total_page'] = ceil($past_zuoye_list['count'] / $this->offset);

		$reward_list = $this->Zuoyes->get_child_zuoye_rewards(0, 3, $conditions);
		if(!empty($reward_list['count'])){
			foreach ($reward_list['list'] as $key => $value) {
				if($value['stars'] > 0 && $value['stars'] < 3){
					$reward_list['list'][$key]['stars'] = '中等';
				}elseif($value['stars'] > 2 && $value['stars'] < 5){
					$reward_list['list'][$key]['stars'] = '优秀';
				}elseif($value['stars'] == 5){
					$reward_list['list'][$key]['stars'] = 'A++';
				}
			}
		}
		$zuoye_statistics = get_field_by_where("user_childrens", "zuoye_stars,zuoye_stars_a,zuoye_stars_b,zuoye_stars_c","id='{$data['child_id']}'");
		echo json_encode(array("code" => 1, "data" => array("zuoye_statistics" => $zuoye_statistics, "today_zuoye_list" => $today_zuoye_list, "past_zuoye_list" => $past_zuoye_list, "reward_list" => $reward_list)));
	}

	function get_past_zuoye_list(){
		$this->load->model("Zuoyes");
		$data = $this->user_post_data;
		$conditions['child_id'] = $data['child_id'];
		$conditions['time_type'] = 2;
		$limit = !empty($data['limit']) ? $data['limit'] : 0;
		$this->offset = 1;
		$past_zuoye_list = $this->Zuoyes->get_child_zuoyes($limit * $this->offset, $this->offset, $conditions);//往期作业
		$past_zuoye_list['total_page'] = ceil($past_zuoye_list['count'] / $this->offset);
		echo json_encode(array("code" => 1, "data" => array("past_zuoye_list" => $past_zuoye_list)));
	}

	function add(){
		$data = $this->user_post_data;
		if(!empty($data)){
			$zuoye = array(
				'childer_id' => $data['childer_id'], 
				'user_id' => $data['user_token']['user_id'], 
				'subject_id' => $data['subject_id'], 
				'title' => $data['title'], 
				'zuoye_week' => date('N', strtotime($data['zuoye_time'])), 
				'zuoye_time' => $data['zuoye_time'], 
                'zuoye_type' => empty($data['zuoye_type'])?1:$data['zuoye_type'],
				'status' => 0, 
				'reward_stars' => 0, 
				'submit_time' => 0, 
				'checked_time' => 0, 
				'add_time' => time(), 
			);
			$this->db->insert('tiku_zuoye',$zuoye);
			
			echo json_encode(array("code" => 1, "data" => null));   // 
			 // echo $userid;
		}else{
			echo json_encode(array("code" => 404, "data" => 'not data'));
		}
	}

	function submit(){
		$data = $this->user_post_data;
		if(!empty($data)){
			$today = date('Y-m-d',time());
			$zuoye = array(
				'status' => 1, 
				'submit_time' => time()
			);
			$this->db->update('tiku_zuoye',$zuoye, array('id' => $data['zuoye_id']));
			
			echo json_encode(array("code" => 1, "data" => null));   // 
			 // echo $userid;
		}else{
			echo json_encode(array("code" => 404, "data" => 'not data'));
		}
	}

	// function check_stars(){
	//     $data = file_get_contents("php://input");
	//     if(!empty($data)){
	//         $data = json_decode($data,true);
	//         $time = time();
	//         $stars = $data['stars'];
	//         $id = $data['zuoye_id'];

	//         $sql = "UPDATE tiku_zuoye SET reward_stars = (reward_stars + {$stars}),checked_time='{$time}',status=2 WHERE id = {$id}";
	//         $this->db->query($sql);

			
			
	//         echo json_encode(array("code" => 1, "data" => null));   // 
	//          // echo $userid;
	//     }else{
	//         echo json_encode(array("code" => 404, "data" => 'not data'));
	//     }
	// }

	function get_zuoye_detail(){
		$data = $this->user_post_data;
		if(!empty($data)){           
			$sql = "SELECT zy.*,sub.name as subject_name,sub.img FROM tiku_zuoye zy left join tiku_b_subject sub on sub.id = zy.subject_id where zy.id={$data['zuoye_id']}";

			$result = $this->db->query($sql)->row_array();
			$result['submit_time'] = $today = date('Y-m-d H:s',$result['submit_time']);
			
			echo json_encode(array("code" => 1, "data" => $result));   // 
			 // echo $userid;
		}else{
			echo json_encode(array("code" => 404, "data" => 'not data'));
		}
	}

	function check(){
		$data = $this->user_post_data;
		if(!empty($data)){
			$time = time();
			$id = $data['zuoye_id'];
			$zuoye_info = get_field_by_where("zuoye", "id,user_id,childer_id,status,subject_id", "id='{$id}'");
			if(!$zuoye_info){
				echo json_encode(array("code" => 0, "data" => '数据不存在'));
				exit;
			}
			if($zuoye_info['user_id'] != $data['user_token']['user_id']){
				echo json_encode(array("code" => 0, "data" => '用户不匹配'));
				exit;
			}
			/*if($zuoye_info['status'] == 2){
				echo json_encode(array("code" => 0, "data" => '已经检查过了，不能重复检查'));
				exit;
			}*/
			if($zuoye_info['user_id'] != $data['user_token']['user_id']){
				echo json_encode(array("code" => 0, "data" => 'user is not matching'));
				exit;
			}
			$image = !empty($data['image']) ? get_qiniu_url($data['image']) : '';
			$zy = array(
				'reward_stars' => $data['stars'],
				'comment' => $data['comment'],
				'image' => $image,
				'checked_time' => $time,
				'status'  => 2
			);
			$this->db->update('zuoye', $zy, array('id' => $id));
			if($this->db->affected_rows() > 0){
				//用户动态数据
				//查询是否存在对应的动态数据
				$user_dynamic_info = get_field_by_where("user_dynamics", "id", "child_id='{$zuoye_info['childer_id']}' AND type = 2 AND data_id='{$id}'");
				$user_dynamic_data['user_id']       = $data['user_token']['user_id'];
				$user_dynamic_data['child_id']      = $zuoye_info['childer_id'];
				$user_dynamic_data['type']          = 2;
				$user_dynamic_data['data_id']       = $id;
				$user_dynamic_data['category_id']   = $zuoye_info['subject_id'];
				$user_dynamic_data['stars']         = $data['stars'];
				$user_dynamic_data['text']          = $data['comment'];
				$user_dynamic_data['image']         = $image;
				$user_dynamic_data['add_time']      = $time;
				if(!empty($user_dynamic_info['id'])){
					//更新动态数据
					$this->db->update("user_dynamics", $user_dynamic_data, array("id" => $user_dynamic_info['id']));
				}else{
					//写入动态数据
					$this->db->insert("user_dynamics", $user_dynamic_data);
				}

				$this->db->set("stars", "stars + " . $data['stars'], false);
				$this->db->where("id", $zuoye_info['childer_id']);
				$this->db->update("user_childrens");
				
				$today_stars_info = get_field_by_where("user_children_today_stars", "*", "child_id='{$zuoye_info['childer_id']}'");
				if(!$today_stars_info){
					$today_stars['user_id'] = $data['user_token']['user_id'];
					$today_stars['child_id'] = $zuoye_info['childer_id'];
					$today_stars['stars'] = $data['stars'];
					$today_stars['last_time'] = $time;
					$this->db->insert("user_children_today_stars", $today_stars);
				}else{
					$today_time = strtotime(date('Y-m-d', $time));
					if($today_stars_info['last_time'] >= $today_time){
						$this->db->set("stars", "stars + " . $data['stars'], false);
						$this->db->set("last_time", $time);
						$this->db->where("id", $today_stars_info['id']);
						$this->db->update("user_children_today_stars");
					}else{
						$this->db->update("user_children_today_stars", array("stars" => $data['stars'], "last_time" => $time), array("id" => $today_stars_info['id']));
					}
				}
			}
			echo json_encode(array("code" => 1, "data" => null));   // 
			 // echo $userid;
		}else{
			echo json_encode(array("code" => 404, "data" => 'not data'));
		}
	}

	function del(){
		$data = $this->user_post_data;
		if(!empty($data)){          
			$id = $data['zuoye_id'];
			$sql = "DELETE FROM tiku_zuoye where id = $id";
			$this->db->query($sql);     
			echo json_encode(array("code" => 1, "data" => null));   // 
			 // echo $userid;
		}else{
			echo json_encode(array("code" => 404, "data" => 'not data'));
		}
	}



	function get_curr_week(){
		$week_start_time = strtotime(date('Y-m-d', time())) - date('w',strtotime(date('Y-m-d', time())))*86400 + 3600 * 24;
		$weeks = array("周一", "周二", "周三", "周四", "周五", "周六", "周日");

		$today_week = date('N', time());

		$week_data = array();
		for($i = 0; $i < 7; $i++){
			$week_data[$i]['id'] = $i + 1;
			$today_start_time = $week_start_time + 3600 * 24 * $i;
			$week_data[$i]['week'] = $weeks[$i];
			$week_data[$i]['date'] = date("Y-m-d", $today_start_time);
			if($today_week == ($i+1)){
				$week_data[$i]['is_today'] = 1;
			}else{
				$week_data[$i]['is_today'] = 0;
			}
		}
		echo json_encode(array("code" => 1, "data" => $week_data));
		
	}


	/**
	 * 获取一周的作业
	 * @return [type] [description]
	 */
	function get_week_zuoye_list_by_child(){
		$data = $this->user_post_data;
		if(!empty($data)){
			//本周星期一
			$week_start_time = strtotime(date('Y-m-d', time())) - date('w',strtotime(date('Y-m-d', time())))*86400 + 3600 * 24;
			$week_start_date = date("Y-m-d", $week_start_time);

			//本周星期天
			$week_end_time = $week_start_time + 3600 * 24 * 7 - 1;
			$week_end_date = date("Y-m-d", $week_end_time);
		   
			
			//查询数据

			$sql = "SELECT zy.*,sub.name as subject_name,sub.img FROM tiku_zuoye zy left join tiku_b_subject sub on sub.id = zy.subject_id where zy.childer_id={$data['childer_id']} and zuoye_time >= '{$week_start_date}' and zuoye_time <= '{$week_end_date}' order by zuoye_week asc,id desc";
			$result = $this->db->query($sql)->result_array();

			
			//处理数据
			$monday = array('week'=>1,'date'=>$week_start_date,'count'=>0,'list'=>array());
		   

			$tuesday = array('week'=>2,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 1),'count'=>0,'list'=>array());


			$wendnesday = array('week'=>3,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 2),'count'=>0,'list'=>array());
			$thursday = array('week'=>4,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 3),'count'=>0,'list'=>array());
			$firday = array('week'=>5,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 4),'count'=>0,'list'=>array());
			$saturday = array('week'=>6,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 5),'count'=>0,'list'=>array());
			$sunday = array('week'=>7,'date'=>date("Y-m-d", $week_start_time + 3600 * 24 * 6),'count'=>0,'list'=>array());

			foreach ($result as $key => $value) {

				if($value['zuoye_week'] == 1){
					
					$monday['list'][] = $value;
				}else if($value['zuoye_week'] == 2){
				   
					$tuesday['list'][] = $value;
				}else if($value['zuoye_week'] == 3){
					
					$wendnesday['list'][] = $value;
				}else if($value['zuoye_week'] == 4){
					
					$thursday['list'][] = $value;
				}else if($value['zuoye_week'] == 5){
					
					$firday['list'][] = $value;
				}else if($value['zuoye_week'] == 6){
					
					$saturday['list'][] = $value;
				}else if($value['zuoye_week'] == 7){
				   
					$sunday['list'][] = $value;
				}
			}

			$monday['count'] = !empty($monday['list']) ? count($monday['list']):0;
			$tuesday['count'] = !empty($tuesday['list']) ? count($tuesday['list']):0;
			$wendnesday['count'] = !empty($wendnesday['list']) ? count($wendnesday['list']):0;
			$thursday['count'] = !empty($thursday['list']) ? count($thursday['list']):0;
			$firday['count'] = !empty($firday['list']) ? count($firday['list']):0;
			$saturday['count'] = !empty($saturday['list']) ? count($saturday['list']):0;
			$sunday['count'] = !empty($sunday['list']) ? count($sunday['list']):0;

			
			$data_obj = array(
				'count' => count($result),
				'list' => array(
					$monday,
					$tuesday,
					$wendnesday,
					$thursday,
					$firday,
					$saturday,
					$sunday),
			 );

			echo json_encode(array("code" => 1, "data" => $data_obj));
		}else{
			echo json_encode(array("code" => 404, "data" => "null"));
		}
	}



}